import { Command } from '../../utils/cli-handler.js';
import { composeColors, color } from '../../../core/color.js';
import { logger } from '../../../core/logger.js';
import { RoboPlaySession } from '../../../roboplay/session.js';
import { RoboPlay } from '../../../roboplay/client.js';

const command = new Command("stop").description("Stop your Robo Pod.").option("-s", "--silent", "do not print anything").option("-v", "--verbose", "print more information for debugging").option("-h", "--help", "Shows the available command options").handler(stopAction);
var stop_default = command;
async function stopAction(_args, options) {
  logger({
    enabled: !options.silent,
    level: options.verbose ? "debug" : "info"
  });
  const session = await RoboPlaySession.get();
  if (!session) {
    logger.error(
      `You must be logged in to deploy to RoboPlay. Run ${composeColors(
        color.bold,
        color.cyan
      )("robo login")} to get started.`
    );
    return;
  }
  const link = session.linkedProjects[process.cwd()];
  if (!link?.podId) {
    logger.error(
      `This project is not linked to a Robo Pod. Run ${composeColors(
        color.bold,
        color.cyan
      )("robo login")} again to fix this.`
    );
    return;
  }
  const pod = session.pods.find((pod2) => pod2.id === link.podId);
  logger.info(`Stopping Pod ${composeColors(color.bold, color.cyan)(pod.name)}...`);
  const result = await RoboPlay.Pod.stop({
    bearerToken: session.userToken,
    podId: pod.id
  });
  if (!result.success) {
    logger.debug(`Failed to stop Pod ${composeColors(color.bold, color.cyan)(pod.name)}.`);
    logger.error(result.error);
    return;
  }
  logger.info(`Successfully stopped Pod ${composeColors(color.bold, color.cyan)(pod.name)}.`);
}

export { stop_default as default };
